package com.example.pkmedical.Fragments.shock;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;


import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Activity.rollovershock.PositionChangeActivity;
import com.example.pkmedical.Base.BaseFragment;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.EventBusUtils;
import com.example.pkmedical.Utils.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class ShockCustomizedFragment extends BaseFragment {
    @BindView(R.id.shock_qiaoji_qiangdu_text)
    TextView shockQiaojiQiangduText;
    @BindView(R.id.shock_qiaoji_qiangdu_button)
    RelativeLayout shockQiaojiQiangduButton;
    @BindView(R.id.shock_qiaoji_pinlv_text)
    TextView shockQiaojiPinlvText;
    @BindView(R.id.shock_qiaoji_pinlv_button)
    RelativeLayout shockQiaojiPinlvButton;
    @BindView(R.id.shock_qiaoji_time_text)
    TextView shockQiaojiTimeText;
    @BindView(R.id.shock_qiaoji_time_button)
    RelativeLayout shockQiaojiTimeButton;
    @BindView(R.id.shock_zhendong_qiangdu_text)
    TextView shockZhendongQiangduText;
    @BindView(R.id.shock_zhendong_qiangdu_button)
    RelativeLayout shockZhendongQiangduButton;
    @BindView(R.id.shock_zhendong_pinlv_text)
    TextView shockZhendongPinlvText;
    @BindView(R.id.shock_zhendong_pinlv_button)
    RelativeLayout shockZhendongPinlvButton;
    @BindView(R.id.shock_zhendong_time_text)
    TextView shockZhendongTimeText;
    @BindView(R.id.shock_zhendong_time_button)
    RelativeLayout shockZhendongTimeButton;
    @BindView(R.id.change_text_layout)
    RelativeLayout changeTextLayout;
    @BindView(R.id. shock_zdy_changebt)
    TextView  shock_zdy_changebt;

    private PopupWindow mStrengthPop, timePop;
    //    private int nowType = 3;//强度当前状态：1：关闭，2：低，3：中，4：高
    int QJqdType = 3;
    int ZDqdType = 3;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_shockcustomized;
    }

    @Override
    public void findViewById(View view) {

    }

    @Override
    public void setViewData(View view) {
//        QJqdType = CommandUtil.SHOCK_QIAOJI_STRENGTH;
//        ZDqdType = CommandUtil.SHOCK_ZHENDONG_STRENGTH;
        shockQiaojiTimeText.setText(SPUtils.getInstance().getInt("shock_time_qiaoji", 10) + getString(R.string.minute));
        shockZhendongTimeText.setText(SPUtils.getInstance().getInt("shock_time_zhendong", 10) + getString(R.string.minute));

        Log.d("yangshuo", QJqdType + "-------" + ZDqdType);
//        switch (QJqdType) {
//            case 1:
//                shockQiaojiQiangduText.setText(R.string.remind_off);
//                break;
//            case 2:
//                shockQiaojiQiangduText.setText(R.string.shock_di);
//                break;
//            case 3:
                shockQiaojiQiangduText.setText(R.string.shock_zhong);
//                break;
//            case 4:
//                shockQiaojiQiangduText.setText(R.string.shock_gao);
//                break;
//
//        }
//        switch (ZDqdType) {
//            case 1:
//                shockZhendongQiangduText.setText(R.string.remind_off);
//                break;
//            case 2:
//                shockZhendongQiangduText.setText(R.string.shock_di);
//                break;
//            case 3:
                shockZhendongQiangduText.setText(R.string.shock_zhong);
//                break;
//            case 4:
//                shockZhendongQiangduText.setText(R.string.shock_gao);
//                break;
//        }

    }

    @Override
    public void setClickEvent(View view) {

    }
//,R.id. shock_zdy_changebt,
    @OnClick({R.id.change_text_layout, R.id.shock_qiaoji_qiangdu_text, R.id.shock_qiaoji_qiangdu_button, R.id.shock_qiaoji_pinlv_text, R.id.shock_qiaoji_pinlv_button, R.id.shock_qiaoji_time_text, R.id.shock_qiaoji_time_button, R.id.shock_zhendong_qiangdu_text, R.id.shock_zhendong_qiangdu_button, R.id.shock_zhendong_pinlv_text, R.id.shock_zhendong_pinlv_button, R.id.shock_zhendong_time_text, R.id.shock_zhendong_time_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shock_qiaoji_qiangdu_text:
                initStrengthWindow(1, QJqdType);
                break;
            case R.id.shock_qiaoji_qiangdu_button:

                break;
            case R.id.shock_qiaoji_pinlv_text:
                initBPSWindow(shockQiaojiPinlvText, 1);
                break;
            case R.id.shock_qiaoji_pinlv_button:
                break;
            case R.id.shock_qiaoji_time_text:
                initTimeWindow(shockQiaojiTimeText, 1);
                break;
            case R.id.shock_qiaoji_time_button:
                break;
            case R.id.shock_zhendong_qiangdu_text:
                initStrengthWindow(2, ZDqdType);
                break;
            case R.id.shock_zhendong_qiangdu_button:
                break;
            case R.id.shock_zhendong_pinlv_text:
                initBPSWindow(shockZhendongPinlvText, 2);
                break;
            case R.id.shock_zhendong_pinlv_button:
                break;
            case R.id.shock_zhendong_time_text:
                initTimeWindow(shockZhendongTimeText, 2);
                break;
            case R.id.shock_zhendong_time_button:
                break;
            case R.id.change_text_layout:
                startActivity(new Intent(MyApp.getAppContext(), PositionChangeActivity.class));
                getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                break;
//            case R.id.shock_zdy_changebt:
//                startActivity(new Intent(MyApp.getAppContext(), PositionChangeActivity.class));
//                getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
//                break;
        }
    }

    /**
     * 是否选择器
     * type:0:敲击，1：震动
     */
    private void initStrengthWindow(int type, int nowType) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_strength_layout, null);
        RelativeLayout close = view.findViewById(R.id.strength_close);
        RelativeLayout low = view.findViewById(R.id.strength_low);
        RelativeLayout secondary = view.findViewById(R.id.strength_secondary);
        RelativeLayout high = view.findViewById(R.id.strength_high);
//        allShow(close,low,secondary,high);
        switch (nowType) {
            case 1:
                close.setVisibility(View.GONE);
                break;
            case 2:
                low.setVisibility(View.GONE);
                break;
            case 3:
                secondary.setVisibility(View.GONE);
                break;
            case 4:
                high.setVisibility(View.GONE);
                break;

        }
        switch (type) {
            case 1:
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockQiaojiQiangduText.setText(R.string.remind_off);
                        QJqdType = 1;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_QIAOJI_STRENGTH = QJqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, QJqdType));
                    }
                });
                low.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockQiaojiQiangduText.setText(R.string.shock_di);
                        QJqdType = 2;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_QIAOJI_STRENGTH = QJqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, QJqdType));
                    }
                });
                secondary.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockQiaojiQiangduText.setText(R.string.shock_zhong);
                        QJqdType = 3;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_QIAOJI_STRENGTH = QJqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, QJqdType));
                    }
                });
                high.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockQiaojiQiangduText.setText(R.string.shock_gao);
                        QJqdType = 4;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_QIAOJI_STRENGTH = QJqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, QJqdType));
                    }
                });

                break;
            case 2:
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockZhendongQiangduText.setText(R.string.remind_off);
                        ZDqdType = 1;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_ZHENDONG_STRENGTH = ZDqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, ZDqdType));
                    }
                });
                low.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockZhendongQiangduText.setText(R.string.shock_di);
                        ZDqdType = 2;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_ZHENDONG_STRENGTH = ZDqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, ZDqdType));
                    }
                });
                secondary.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockZhendongQiangduText.setText(R.string.shock_zhong);
                        ZDqdType = 3;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_ZHENDONG_STRENGTH = ZDqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, ZDqdType));
                    }
                });
                high.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shockZhendongQiangduText.setText(R.string.shock_gao);
                        ZDqdType = 4;
                        mStrengthPop.dismiss();
                        CommandUtil.SHOCK_ZHENDONG_STRENGTH = ZDqdType;
                        EventBusUtils.post(MessageWrap.getInstance(MessageWrap.SHOCK_ROLLOVER_NEXT, ZDqdType));
                    }
                });

                break;

        }

        mStrengthPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mStrengthPop.setFocusable(true);
        mStrengthPop.setOutsideTouchable(true);
        mStrengthPop.setBackgroundDrawable(new ColorDrawable());
        switch (type) {
            case 1:
                mStrengthPop.showAsDropDown(shockQiaojiQiangduButton);
                break;
            case 2:
                mStrengthPop.showAsDropDown(shockZhendongQiangduButton);
                break;
        }


    }

    /**
     * 时间选择
     *
     * @param button
     * @param o  1:敲击，2：震动
     */
    private int timeNow;

    private void initTimeWindow(final TextView button, final int o) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_time, null);
        TextView tv_it_time = view.findViewById(R.id.tv_it_time);
        ImageView iv_it_left = view.findViewById(R.id.iv_it_left);
        ImageView iv_it_right = view.findViewById(R.id.iv_it_right);
        SeekBar sb_it = view.findViewById(R.id.sb_it);
        String btStr = button.getText().toString();
        timeNow = Integer.parseInt(btStr.substring(0, btStr.indexOf(getString(R.string.minute))));
        tv_it_time.setText(timeNow + "");
        sb_it.setProgress(timeNow);


        iv_it_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (timeNow > 3) {
                    timeNow = timeNow - 1;
                    tv_it_time.setText(timeNow + "");
                    button.setText(timeNow + getString(R.string.minute));
                    sb_it.setProgress((timeNow));
                    switch (o) {
                        case 1:
                            SPUtils.getInstance().put("shock_time_qiaoji", timeNow);
                            break;
                        case 2:
                            SPUtils.getInstance().put("shock_time_zhendong", timeNow);
                            break;
                    }
                }
            }
        });
        iv_it_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (timeNow < 30) {
                    timeNow = timeNow + 1;
                    tv_it_time.setText(timeNow + "");
                    button.setText(timeNow + getString(R.string.minute));
                    sb_it.setProgress(timeNow);
                    switch (o) {
                        case 1:
                            SPUtils.getInstance().put("shock_time_qiaoji", timeNow);
                            break;
                        case 2:
                            SPUtils.getInstance().put("shock_time_zhendong", timeNow);
                            break;
                    }
                }
            }
        });
        sb_it.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (i > 3) {
                    timeNow = i / 1;
                    button.setText(timeNow + getString(R.string.minute));
                    tv_it_time.setText(timeNow + "");
                } else{
                    seekBar.setProgress(3);
                    timeNow = 3;
                    button.setText(timeNow + getString(R.string.minute));
                    tv_it_time.setText(timeNow + "");
                }
                switch (o) {
                    case 1:
                        SPUtils.getInstance().put("shock_time_qiaoji", timeNow);
                        break;
                    case 2:
                        SPUtils.getInstance().put("shock_time_zhendong", timeNow);
                        break;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        timePop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        timePop.setFocusable(true);
        timePop.setOutsideTouchable(true);
        timePop.setBackgroundDrawable(new ColorDrawable());
        int xoff = (timePop.getContentView().getMeasuredWidth() - button.getWidth()) / 2;
        timePop.showAsDropDown(button, -xoff, 0);
    }

    /**
     * bps
     *
     * @param button
     * @param o  1:敲击，2：震动
     */
    private int bpsNow;

    private void initBPSWindow(final TextView button, int o) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_time, null);
        final TextView tv_it_time = view.findViewById(R.id.tv_it_time);
        ImageView iv_it_left = view.findViewById(R.id.iv_it_left);
        ImageView iv_it_right = view.findViewById(R.id.iv_it_right);
        final SeekBar sb_it = view.findViewById(R.id.sb_it);
        String btStr = button.getText().toString();
        bpsNow = Integer.parseInt(btStr.substring(0, btStr.indexOf("bps")));
        tv_it_time.setText(bpsNow + "");
        sb_it.setProgress(bpsNow);


        iv_it_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bpsNow > 3 ) {
                    bpsNow -=  1;
                    tv_it_time.setText(bpsNow + "");
                    button.setText(bpsNow + "bps");
                    sb_it.setProgress(bpsNow);

                    switch (o) {
                        case 1:
                            CommandUtil.SHOCK_QIAOJI_BPS = bpsNow;
                            break;
                        case 2:
                            CommandUtil.SHOCK_ZHENDONG_BPS = bpsNow;
                            break;
                    }
                }
            }
        });
        iv_it_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bpsNow < 30) {
                    bpsNow = bpsNow + 1;
                    tv_it_time.setText(bpsNow + "");
                    button.setText(bpsNow + "bps");
                    sb_it.setProgress(bpsNow);
                    switch (o) {
                        case 1:
                            CommandUtil.SHOCK_QIAOJI_BPS = bpsNow;
                            break;
                        case 2:
                            CommandUtil.SHOCK_ZHENDONG_BPS = bpsNow;
                            break;
                    }
                }
            }
        });
        sb_it.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (i > 3) {
                    bpsNow = i / 1;
                    button.setText(bpsNow + "bps");
                    tv_it_time.setText(bpsNow + "");
                } else {
                    seekBar.setProgress(3);
                    bpsNow = 3;
                    button.setText(bpsNow + "bps");
                    tv_it_time.setText(bpsNow + "");
                }
                switch (o) {
                    case 1:
                        CommandUtil.SHOCK_QIAOJI_BPS = bpsNow;
                        break;
                    case 2:
                        CommandUtil.SHOCK_ZHENDONG_BPS = bpsNow;
                        break;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        timePop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        timePop.setFocusable(true);
        timePop.setOutsideTouchable(true);
        timePop.setBackgroundDrawable(new ColorDrawable());
        int xoff = (timePop.getContentView().getMeasuredWidth() - button.getWidth()) / 2;
        timePop.showAsDropDown(button, -xoff, 0);
    }
}
